{extend name='layout:app'}

{block name='css'}
<link rel="stylesheet" href="__STATIC__/css/dropload.css">
<script src="__STATIC__/js/dropload.js"></script>
<link rel="stylesheet" href="__STATIC__/css/voucher.css">
{/block}

{block name='content'}
    <div id="app">
        <div class="nav">
            <i class="el-icon-arrow-left icon_left" @click="back"></i>
            <div>我的奖品</div>
            <i class="iconfont icon-home icon_right" @click="home"></i>
        </div>

        <div class="voucher">
            <div class="voucher-sort" @click="voucherSort">
                <div class="sort active">未兑换</div>
                <div class="sort">已兑换</div>
            </div>
            <div class="voucher-list" v-cloak>
                <div class="voucher-cont">
                    <div class="list" v-for="prize in voucherList">
                        <!--<div class="item item-left">
                            &lt;!&ndash;<div class="icon-voucher">{{prize.coup_price}}元</div>&ndash;&gt;
                            <div class="condition">{{prize.name}}</div>
                        </div>-->
                        <div class="item">
                            <div class="prize-name">{{prize.name}}</div>
                            <div class="voucher-info">{{prize.code}}</div>
                            <div class="voucher-time">{{prize.time}}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{/block}

{block name='js'}
<script>
    var page = -1
    var size = 40
    var that;

    new Vue({
        el:'#app',
        data: {
            vouchers: '',
            voucherList: []
        },
        created() {
            that = this
            this.$nextTick(() => {
                // dropload
                that.getVoucher(0)
            })
        },
        methods: {
            back() {
                window.history.go(-1)
            },
            home() {
                location.href = '/'
            },
            voucherSort(e) {
                const voucherSort = e.target.innerText
                $('.dropload-down').remove()  //清除信息提示dom
                page = -1  //page重置
                this.voucherList = [] //清空列表

                if(voucherSort === '未兑换') {
                    this.getVoucher(0)
                }else {
                    this.getVoucher(1)

                }
                setTimeout(() => {
                    if($('.dropload-refresh').length) {
                        $(e.target).click()
                    }
                }, 50)
                $(e.target).addClass('active')
                $(e.target).siblings().removeClass('active')
            },
            getVoucher(_status) {
                $('.voucher-list').dropload({
                    scrollArea : window,
                    threshold : 50, //提前加载距离
                    autoLoad: true,
                    domUp: [],
                    loadDownFn : function(me){
                        page++;
                        $.ajax({
                            type: 'GET',
                            url: '/index/index/getVoucher?page='+page,
                            data: {status: _status},
                            dataType: 'json',
                            success: function(data){
                                var arrLen = data.length;
                                if(arrLen) {
                                    for(v of data) {
                                        that.voucherList.push(
                                            {
                                                code: v.awar_code,
                                                name: v.awar_name,
                                                time: v.awar_time
                                            }
                                        )
                                    }
                                    if(arrLen < 10) {
                                        me.lock();
                                        me.noData();
                                        me.resetload();
                                        return false
                                    }
                                }else{  // 如果没有数据
                                    // 锁定
                                    me.lock();
                                    // 无数据
                                    me.noData();
                                }
                                // 为了测试，延迟1秒加载
                                /*setTimeout(function(){
                                    // 插入数据到页面，放到最后面
                                    // $('.lists').append(result);
                                    // 每次数据插入，必须重置
                                    // me.resetload();
                                },1000);*/
                                me.resetload();
                            },
                            error: function(xhr, type){
                                alert('Ajax error!');
                                // 即使加载出错，也得重置
                                me.resetload();
                            }
                        });
                    }
                });
            }
        }
    });





</script>
{/block}